package com.example.mediarecorderdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import com.example.mediarecorderdemo.PlayAudioFragment.Sound;
import com.example.mediarecorderdemo.databinding.AudioItemBinding;
import java.util.ArrayList;
import java.util.List;

public class AudioAdapter extends Adapter<AudioAdapter.MyViewHolder> {
    private List<Sound> mDataList = new ArrayList();
    private Context mContext;
    private AudioItemBinding mAudioItemBinding;
    private OnRecyclerItemClickListener mOnItemClickListener;

    public AudioAdapter(List<Sound> dataList, Context context) {
        this.mDataList = dataList;
        this.mContext = context;
    }

    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        this.mAudioItemBinding = DataBindingUtil.inflate(LayoutInflater.from(this.mContext), R.layout.audio_item, parent, false);
        return new MyViewHolder(this.mAudioItemBinding);
    }

    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.mAudioItemBinding.textView.setText((mDataList.get(position)).getName());
    }

    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    public void setOnRecyclerItemClickListener(OnRecyclerItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public interface OnRecyclerItemClickListener {
        void onItemClick(int position, String name);
    }

    public class MyViewHolder extends ViewHolder {
        private AudioItemBinding mAudioItemBinding;

        public MyViewHolder(AudioItemBinding binding) {
            super(binding.getRoot());
            this.mAudioItemBinding = binding;
            this.mAudioItemBinding.textView.setOnClickListener((v) -> {
                AudioAdapter.this.mOnItemClickListener.onItemClick(this.getLayoutPosition(), (String)this.mAudioItemBinding.textView.getText());
            });
        }
    }
}
